package com.problem.leetcode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author didi
 * @DESCRIPTION
 * @create 2019-05-02 12:58
 **/

public class Problem819 {

    public String mostCommonWord(String paragraph, String[] banned) {

        HashSet<String> banedSet = new HashSet<>();
        for (String ele : banned) {
            banedSet.add(ele);
        }

        HashMap<String, Integer> hashSet = new HashMap<>();
        String                   pattern = "([a-z]|[A-Z])+";
        Pattern                  r       = Pattern.compile(pattern);
        Matcher                  m       = r.matcher(paragraph);

        while (m.find()) {
            String ele = m.group().toLowerCase();
            if (!hashSet.containsKey(ele)) {
                hashSet.put(ele, 0);
            } else {
                hashSet.put(ele, hashSet.get(ele) + 1);
            }
        }

        int    max    = Integer.MIN_VALUE;
        String result = "";
        for (Map.Entry<String, Integer> entry : hashSet.entrySet()) {
            if (!banedSet.contains(entry.getKey())) {
                if (entry.getValue() > max) {
                    max = entry.getValue();
                    result = entry.getKey();
                }
            }
        }

        return result;
    }
}
